package com.ruoyi.plus.pub.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.api.ApiResult;
import com.ruoyi.common.core.api.BaseApi;
import com.ruoyi.common.core.api.IdsEntity;
import com.ruoyi.common.core.api.version.ApiVersion;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.plus.constants.PlusDictConstants;
import com.ruoyi.common.utils.PageUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.plus.pub.api.po.pubNotice.PubNoticeEntityPo;
import com.ruoyi.plus.pub.api.po.pubNotice.PubNoticePlusPo;
import com.ruoyi.plus.pub.api.po.pubNotice.PubNoticePlusQueryPo;
import com.ruoyi.plus.pub.api.po.pubNotice.PubNoticeUserEntityPo;
import com.ruoyi.plus.pub.api.vo.PubNoticePlusVo;
import com.ruoyi.plus.pub.domain.PubNoticePlus;
import com.ruoyi.plus.pub.domain.PubNoticeReceiver;
import com.ruoyi.plus.pub.service.IPubNoticePlusService;
import com.ruoyi.plus.pub.service.IPubNoticeReceiverService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 系统公告通知Api
 *
 */
@Slf4j
@Api(tags = "pubNoticePlusApi-系统公告通知")
@ApiVersion()
@RestController
@RequestMapping("app/api/pub/pubNoticePlusApi")
public class PubNoticePlusApi extends BaseApi {

    @Autowired
    private IPubNoticePlusService pubNoticePlusService;
    @Autowired
    private IPubNoticeReceiverService pubNoticeReceiverService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 获取系统公告通知对象
     */
    @ApiOperation("get-获取系统公告通知对象")
    @PostMapping({"/get"})
    @ResponseBody
    public ApiResult<PubNoticePlusVo> get(@Validated @RequestBody PubNoticeEntityPo po) {
        PubNoticePlus entity = pubNoticePlusService.getById(po.getNoticeId());
        if (null == entity) {
            return new ApiResult<PubNoticePlusVo>(getApiVersion()).setCode(-1).setMsg("您所查询的id{" + po.getNoticeId() + "}不存在!").setData(null).setApiVersion(this.getApiVersion());
        }
        if (entity.getType().equals(PlusDictConstants.NOTICE_PLUS_TYPE_PRIVATE) && po.getUserId() != null) {
            //如果是通知并且userId不为空,修改阅读状态
            PubNoticeReceiver pubNoticeReceiver = new PubNoticeReceiver();
            pubNoticeReceiver.setNoticeId(entity.getId());
            pubNoticeReceiver.setUserId(po.getUserId());
            pubNoticeReceiver.setReadStatus(PlusDictConstants.NOTICE_PLUS_READ_STATUS_READ);
            pubNoticeReceiverService.updateReadStatus(pubNoticeReceiver);
        }
        entity.setReadStatus(PlusDictConstants.NOTICE_PLUS_READ_STATUS_READ);

        PubNoticePlusVo vo = new PubNoticePlusVo();
        BeanUtils.copyProperties(entity, vo);
        return new ApiResult<PubNoticePlusVo>(getApiVersion()).setData(vo).setApiVersion(this.getApiVersion());
    }

    /**
     * 查询系统公告通知列表
     */
    @ApiOperation("list-获取系统公告通知列表")
    @PostMapping("/list")
    @ResponseBody
    public ApiResult<PageInfo<PubNoticePlusVo>> list(@Validated @RequestBody PubNoticePlusQueryPo pubNoticePlusQueryPo) {

        String type = pubNoticePlusQueryPo.getType();
        if (StringUtils.isEmpty(type)) {
            type = PlusDictConstants.NOTICE_PLUS_TYPE_PRIVATE;
        } else if (!type.equals(PlusDictConstants.NOTICE_PLUS_TYPE_PUBLIC) && !type.equals(PlusDictConstants.NOTICE_PLUS_TYPE_PRIVATE)) {
            type = PlusDictConstants.NOTICE_PLUS_TYPE_PRIVATE;
        }

        PubNoticePlus pubNoticePlus = new PubNoticePlus();
        BeanUtils.copyProperties(pubNoticePlusQueryPo, pubNoticePlus);

        PageHelper.startPage(pubNoticePlusQueryPo.getPageNum(), pubNoticePlusQueryPo.getPageSize());

        List<PubNoticePlus> list = new ArrayList<>();
        //获取公告
        if (PlusDictConstants.NOTICE_PLUS_TYPE_PUBLIC.equals(type)) {
            list = pubNoticePlusService.selectPubNoticePlusList(pubNoticePlus);
        }
        //获取通知
        if (PlusDictConstants.NOTICE_PLUS_TYPE_PRIVATE.equals(type)) {
            list = pubNoticePlusService.apiGetNotices(pubNoticePlus);
        }

        PageInfo<PubNoticePlus> pageInfo = new PageInfo<>(list);

        if (list.size() == 0) {
            return new ApiResult<PageInfo<PubNoticePlusVo>>(getApiVersion()).setCode(-1).setMsg("未查到内容!").setData(null).setApiVersion(this.getApiVersion());
        }

        PageInfo<PubNoticePlusVo> pageInfoVo = PageUtil.copyToPageInfoVo(pageInfo, PubNoticePlusVo::new);
        return new ApiResult<PageInfo<PubNoticePlusVo>>(getApiVersion()).setData(pageInfoVo).setApiVersion(this.getApiVersion());
    }

    /**
     * 获取用户未读通知数
     *
     * @param po
     * @return
     */
    @ApiOperation("count-获取用户未读通知数")
    @PostMapping("/count")
    @ResponseBody
    public ApiResult<Integer> unReadNoticeCount(@Validated @RequestBody PubNoticeUserEntityPo po) {
        return new ApiResult<Integer>(getApiVersion()).setData(pubNoticeReceiverService.unReadCount(po.getUserId(), po.getSystemType())).setApiVersion(this.getApiVersion());
    }


    /**
     * 新增保存公告通知
     */
    @ApiOperation("新增保存公告通知")
    @Log(title = "add-公告通知", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public ApiResult<Boolean> addSave(@Validated @RequestBody PubNoticePlusPo pubNoticePlusPo) {
        PubNoticePlus pubNoticePlus = buildCopyClass(pubNoticePlusPo, PubNoticePlus.class);
        return new ApiResult<Boolean>(getApiVersion()).setData(pubNoticePlusService.save(pubNoticePlus)).setApiVersion(this.getApiVersion());
    }

    /**
     * 修改保存公告通知
     */
    @ApiOperation("修改保存公告通知")
    @Log(title = "公告通知", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public ApiResult<Boolean> editSave(@Validated @RequestBody PubNoticePlusPo pubNoticePlusPo) {
        PubNoticePlus pubNoticePlus = buildCopyClass(pubNoticePlusPo, PubNoticePlus.class);
        return new ApiResult<Boolean>(getApiVersion()).setData(pubNoticePlusService.saveOrUpdate(pubNoticePlus)).setApiVersion(this.getApiVersion());
    }

    /**
     * 删除公告通知
     */
    @ApiOperation("remove-删除公告通知")
    @Log(title = "公告通知", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public ApiResult<Boolean> remove(@RequestBody IdsEntity IdsEntity) {
        return new ApiResult<Boolean>(getApiVersion()).setData(pubNoticePlusService.removeByIds(Arrays.asList(Convert.toLongArray(IdsEntity.getIds())))).setApiVersion(this.getApiVersion());
    }

    @ApiOperation(value = "changeStatus-发布/撤回公告", notes = "2发布,3撤回")
    @GetMapping("/changeStatus/{noticeId}/{status}")
    @Log(title = "changeStatus-发布/撤回公告", businessType = BusinessType.UPDATE)
    @ResponseBody
    public ApiResult<Boolean> changeFeedbackStatus(@ApiParam(value = "公告或通知id", required = true) @PathVariable(value = "noticeId") Long noticeId,
                                                   @ApiParam(value = "状态(2发布,3撤回)", required = true) @PathVariable("status") String status) {
        PubNoticePlus noticePlus = new PubNoticePlus();
        noticePlus.setId(noticeId).setStatus(status);
        Boolean result = pubNoticePlusService.updateById(noticePlus);
        return new ApiResult<Boolean>(getApiVersion()).setMsg("操作成功!").setData(result).setApiVersion(this.getApiVersion());
    }

    @ApiOperation(value = "readAll-一键已读", notes = "系统类型")
    @GetMapping("/readAll/{system}")
    @Log(title = "readAll-一键已读", businessType = BusinessType.UPDATE)
    @ResponseBody
    public ApiResult<Boolean> readAll(@ApiParam(value = "系统类型", required = true) @PathVariable(value = "system") String system) {
        pubNoticePlusService.readAll(system);
        return new ApiResult<Boolean>(getApiVersion()).setMsg("操作成功!").setData(true).setApiVersion(this.getApiVersion());
    }

}
